package com.anxin.registerManagement.controller;

import com.anxin.registerManagement.bean.jo.AcademySearchJo;
import com.anxin.registerManagement.bean.persist.Academy;
import com.anxin.registerManagement.service.AcademyService;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 叶前呈
 * @date 2022/1/29 12:17
 */
@Api(tags = "学院接口")
@RestController
@RequestMapping("/academies")
public class AcademyController {

    private static final Logger logger = LoggerFactory.getLogger(AcademyController.class);

    @Autowired
    AcademyService academyService;

    @ApiOperation("查询学院")
    @PostMapping("/search")
    public ResultUtil getAcademyList(@RequestBody AcademySearchJo academySearchJo) {
        logger.info("查询学院：{}", academySearchJo);
        return ResultUtil.success(academyService.getAcademyList(academySearchJo));
    }


    @ApiOperation("添加学院")
    @PostMapping("/add")
    public ResultUtil addAcademy(@Valid @RequestBody Academy academy) {
        logger.info("添加学院：{}", academy);
        academyService.addAcademy(academy);
        return ResultUtil.success();
    }

    @ApiOperation("修改学院")
    @PostMapping("/update")
    public ResultUtil updateAcademy(@Valid @RequestBody Academy academy) {
        Assert.notNull(academy.getId(), "学院id不能为空");
        academyService.updateAcademy(academy);
        return ResultUtil.success();
    }

    @ApiOperation("删除学院")
    @PostMapping("/delete")
    public ResultUtil deleteAcademy(@RequestParam("id") int id) {
        Assert.notNull(id, "id不能为空");
        academyService.deleteAcademy(id);
        return ResultUtil.success();
    }

    @ApiOperation("获取所有学院")
    @GetMapping("/get")
    public ResultUtil getAllAcademy() {
        return ResultUtil.success(academyService.getAllAcademy());
    }
}
